package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.SysDevice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDeviceEarlyWarning;
import com.ruoyi.system.service.ISysDeviceEarlyWarningService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备预警单Controller
 * 
 * @author ruoyi
 * @date 2022-07-21
 */
@Api("设备预警单管理")
@Controller
@RequestMapping("/system/warning")
public class SysDeviceEarlyWarningController extends BaseController
{
    private String prefix = "system/warning";

    @Autowired
    private ISysDeviceEarlyWarningService sysDeviceEarlyWarningService;

    @RequiresPermissions("system:warning:view")
    @GetMapping()
    public String warning()
    {
        return prefix + "/warning";
    }

    /**
     * 查询设备预警单列表
     */
    @ApiOperation("查询设备预警单列表")
    @RequiresPermissions("system:warning:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDeviceEarlyWarning sysDeviceEarlyWarning)
    {
        startPage();
        List<SysDeviceEarlyWarning> list = sysDeviceEarlyWarningService.selectSysDeviceEarlyWarningList(sysDeviceEarlyWarning);
        return getDataTable(list);
    }

    /**
     * 导出设备预警单列表
     */
    @ApiOperation("导出设备预警单列表")
    @RequiresPermissions("system:warning:export")
    @Log(title = "设备预警单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDeviceEarlyWarning sysDeviceEarlyWarning)
    {
        List<SysDeviceEarlyWarning> list = sysDeviceEarlyWarningService.selectSysDeviceEarlyWarningList(sysDeviceEarlyWarning);
        ExcelUtil<SysDeviceEarlyWarning> util = new ExcelUtil<SysDeviceEarlyWarning>(SysDeviceEarlyWarning.class);
        return util.exportExcel(list, "设备预警单数据");
    }

    /**
     * 新增设备预警单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备预警单
     */
    @ApiOperation("新增保存设备预警单")
    @RequiresPermissions("system:warning:add")
    @Log(title = "设备预警单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDeviceEarlyWarning sysDeviceEarlyWarning)
    {
        return toAjax(sysDeviceEarlyWarningService.insertSysDeviceEarlyWarning(sysDeviceEarlyWarning));
    }

    /**
     * 修改设备预警单
     */
    @RequiresPermissions("system:warning:edit")
    @GetMapping("/edit/{dewId}")
    public String edit(@PathVariable("dewId") Long dewId, ModelMap mmap)
    {
        SysDeviceEarlyWarning sysDeviceEarlyWarning = sysDeviceEarlyWarningService.selectSysDeviceEarlyWarningByDewId(dewId);
        mmap.put("sysDeviceEarlyWarning", sysDeviceEarlyWarning);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备预警单
     */
    @ApiOperation("修改保存设备预警单")
    @RequiresPermissions("system:warning:edit")
    @Log(title = "设备预警单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDeviceEarlyWarning sysDeviceEarlyWarning)
    {
        return toAjax(sysDeviceEarlyWarningService.updateSysDeviceEarlyWarning(sysDeviceEarlyWarning));
    }

    /**
     * 删除设备预警单
     */
    @ApiOperation("删除设备预警单")
    @RequiresPermissions("system:warning:remove")
    @Log(title = "设备预警单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysDeviceEarlyWarningService.deleteSysDeviceEarlyWarningByDewIds(ids));
    }

    /**
     * 导入设备预警单
     */
    @ApiOperation("导入设备预警单")
    @RequiresPermissions("system:warning:import")
    @Log(title = "设备预警单", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(@RequestPart MultipartFile file, @RequestParam(defaultValue = "false") boolean updateSupport) throws Exception
    {
        ExcelUtil<SysDeviceEarlyWarning> util = new ExcelUtil<SysDeviceEarlyWarning>(SysDeviceEarlyWarning.class);
        List<SysDeviceEarlyWarning> list = util.importExcel(file.getInputStream());
        String message = sysDeviceEarlyWarningService.importDeviceEarlyWarning(list, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 导出设备预警单模板
     */
    @ApiOperation("导出设备预警单模板")
    @RequiresPermissions("system:warning:view")
    @Log(title = "设备预警单", businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysDeviceEarlyWarning> util = new ExcelUtil<SysDeviceEarlyWarning>(SysDeviceEarlyWarning.class);
        return util.importTemplateExcel("物资信息");
    }
}
